package com.yh.wuye.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.TimeChart;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class LineChart extends BaseActivity
{
  private Button btnBack;
  private ViewGroup layoutChart;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.pie_chart);
    
    btnBack = (Button)findViewById(R.id.btnBack);
    btnBack.setOnClickListener(onBack);
    
    layoutChart = (ViewGroup)findViewById(R.id.layoutChart);
    
    loadChart();
  }
  
  private OnClickListener onBack = new OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
      finish();
    }
  };

  private void loadChart()
  {

    String[] titles = new String[] { "水费", "电费" };
    List<Date[]> dates = new ArrayList<Date[]>();
    List<double[]> values = new ArrayList<double[]>();
    int length = titles.length;
    for (int i = 0; i < length; i++) {
      dates.add(new Date[12]);
      dates.get(i)[0] = new Date(111, 9, 1);
      dates.get(i)[1] = new Date(111, 9, 8);
      dates.get(i)[2] = new Date(111, 9, 15);
      dates.get(i)[3] = new Date(111, 9, 22);
      dates.get(i)[4] = new Date(111, 9, 29);
      dates.get(i)[5] = new Date(111, 10, 5);
      dates.get(i)[6] = new Date(111, 10, 12);
      dates.get(i)[7] = new Date(111, 10, 19);
      dates.get(i)[8] = new Date(111, 10, 26);
      dates.get(i)[9] = new Date(111, 11, 3);
      dates.get(i)[10] = new Date(111, 11, 10);
      dates.get(i)[11] = new Date(111, 11, 17);
    }
    values.add(new double[] { 142, 123, 142, 152, 149, 122, 110, 120, 125, 155, 146, 150 });
    values.add(new double[] { 102, 90, 112, 105, 125, 112, 125, 112, 105, 115, 116, 135 });
    length = values.get(0).length;
    int[] colors = new int[] { Color.CYAN, Color.GREEN };
    PointStyle[] styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT };
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    setChartSettings(renderer, "", "", "", dates.get(0)[0].getTime(),
        dates.get(0)[11].getTime(), 50, 190, Color.GRAY, Color.LTGRAY);
    renderer.setXLabels(5);
    renderer.setYLabels(10);
    renderer.setDisplayChartValues(true);

//    org.achartengine.chart.LineChart l = new org.achartengine.chart.LineChart(
//        buildDateDataset(titles, dates, values), renderer);

    TimeChart chart = new TimeChart(buildDateDataset(titles, dates, values), renderer);
    chart.setDateFormat("yyyy-MM-dd");
    
    
    GraphicalView gv = new GraphicalView(this, chart);
    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    gv.setLayoutParams(params);
    layoutChart.addView(gv);
    
  };
  
  private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(16);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setPointSize(5f);
    renderer.setMargins(new int[] { 20, 30, 15, 0 });
    int length = colors.length;
    for (int i = 0; i < length; i++) {
      XYSeriesRenderer r = new XYSeriesRenderer();
      r.setColor(colors[i]);
      r.setPointStyle(styles[i]);
      renderer.addSeriesRenderer(r);
    }
    return renderer;
  }
  
  private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
      String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
      int labelsColor) {
    renderer.setChartTitle(title);
    renderer.setXTitle(xTitle);
    renderer.setYTitle(yTitle);
    renderer.setXAxisMin(xMin);
    renderer.setXAxisMax(xMax);
    renderer.setYAxisMin(yMin);
    renderer.setYAxisMax(yMax);
    renderer.setAxesColor(axesColor);
    renderer.setLabelsColor(labelsColor);

  }

  private XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues,
      List<double[]> yValues) {
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    int length = titles.length;
    for (int i = 0; i < length; i++) {
      TimeSeries series = new TimeSeries(titles[i]);
      Date[] xV = xValues.get(i);
      double[] yV = yValues.get(i);
      int seriesLength = xV.length;
      for (int k = 0; k < seriesLength; k++) {
        Date d = xV[k];
        series.add(d, yV[k]);
      }
      dataset.addSeries(series);
    }
    return dataset;
  }

}
